package com.pocketsweet.ui.uilib.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pocketsweet.R;
import com.pocketsweet.model.MLGift;
import com.pocketsweet.service.UserService;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

import java.util.List;

/**
 * 
 * @author Kam
 * 
 */
public class GiftStoreAdapter extends BaseListAdapter<MLGift> {
	private Context context;

	public GiftStoreAdapter(Context ctx) {
		super(ctx);
		this.context = ctx;
	}

	public GiftStoreAdapter(Context ctx, List<MLGift> datas) {
		super(ctx, datas);
		this.context = ctx;
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_gift, parent, false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();

		}
		viewHolder.refreshView(datas.get(position), position);
		return convertView;
	}

	public class ViewHolder {
		ImageView imgGift;
		TextView tvPrice;
		TextView tvGiftName;
		TextView tvGiftNum;
		TextView tvGiftInfo;
		MLGift item;
		int likeCount = 0;
		public View parent;

		public ViewHolder(View parent) {
			this.parent = parent;
			initView(parent);
		}

		public void initView(View parent) {
			imgGift = (ImageView) parent.findViewById(R.id.imgGift);
			tvPrice = (TextView) parent.findViewById(R.id.tvPrice);
			tvGiftName = (TextView) parent.findViewById(R.id.tvGiftName);
			tvGiftInfo = (TextView) parent.findViewById(R.id.tvGiftInfo);
			tvGiftNum = (TextView) parent.findViewById(R.id.tvGiftNum);
		}

		public void refreshView(MLGift item, int i) {
			this.item = item;
			// 设置图片
			if (item != null) {
				String avatar = item.getImgUrl();
				if (avatar != null && !avatar.equals("")) {
					UserService.displaySquare(avatar, imgGift);
				} else {
					imgGift.setImageResource(R.drawable.default_user_avatar);
				}
				if (!StringUtils.isEmpty((item.getName()))) {
					tvGiftName.setText(item.getName());
				}
				tvGiftInfo.setText(item.getInfo());
				tvPrice.setText(String.valueOf((int)item.getPrice())+"时光");
			}
		}

	}

}
